package com.pisolution.scheduler.runtime.context;

import com.pisolution.scheduler.datasource.instance.TaskInstance;
import com.pisolution.scheduler.datasource.instance.WorkflowInstance;
import com.pisolution.scheduler.datasource.workflow.Workflow;

/**
 * 用于辅助构建 {@link TaskExecutionContext}.
 *
 * @author sora
 * @version 0.1.0
 */
public final class TaskExecutionContextBuilder {
    /** 任务执行上下文环境. */
    private final TaskExecutionContext context;

    // -------------------------------------------------------------------------------------------------
    //  初始化
    // -------------------------------------------------------------------------------------------------

    public TaskExecutionContextBuilder() {
        this.context = new TaskExecutionContext();
    }

    // -------------------------------------------------------------------------------------------------
    //  属性访问
    // -------------------------------------------------------------------------------------------------

    public TaskExecutionContext create() {
        return this.context;
    }

    // -------------------------------------------------------------------------------------------------
    //  构建上下文
    // -------------------------------------------------------------------------------------------------

    /**
     * 设置工作流相关信息.
     *
     * @param workflow 任务所属工作流.
     *
     * @return 当前对象以便链式调用.
     */
    public TaskExecutionContextBuilder buildWorkflow(final Workflow workflow) {
        this.context.setProjectId(workflow.getProjectId());
        this.context.setWorkflowId(workflow.getId());

        return this;
    }

    /**
     * 设置工作流实例相关信息.
     *
     * @param instance 任务所属工作流实例.
     *
     * @return 当前对象以便链式调用.
     */
    public TaskExecutionContextBuilder buildWorkflowInstance(final WorkflowInstance instance) {
        this.context.setWorkflowInstanceId(instance.getId());
        this.context.setScheduleTime(instance.getScheduleTime());
        this.context.setUserId(instance.getUserId());

        return this;
    }

    /**
     * 设置任务实例相关信息.
     *
     * @param instance 任务实例.
     *
     * @return 当前对象以便链式调用.
     */
    public TaskExecutionContextBuilder buildTaskInstance(final TaskInstance instance) {
        this.context.setId(instance.getId());
        this.context.setName(instance.getName());
        this.context.setType(instance.getType());
        this.context.setContent(instance.getContent());
        this.context.setHost(instance.getHost());
        this.context.setWorkerGroup(instance.getWorkerGroup());
        this.context.setExecutePath(instance.getExecutePath());
        this.context.setLogPath(instance.getLogPath());
        this.context.setStartTime(instance.getStartTime());

        return this;
    }
}
